package chzhenjia.learn.androidcode.dialog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import chzhenjia.learn.androidcode.R;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        Button btnCancelOk = (Button) findViewById(R.id.btn_cancel_ok);
        Button btnCheckbox = (Button) findViewById(R.id.btn_checkbox);
        Button btnRadio = (Button) findViewById(R.id.btn_radio);
        btnCancelOk.setOnClickListener(this);
        btnCheckbox.setOnClickListener(this);
        btnRadio.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel_ok:
                showCancelOkDialog();
                break;
            case R.id.btn_checkbox:
                showCheckboxDialog();
                break;
            case R.id.btn_radio:
                showRadioDialog();
                break;
        }
    }

    /**
     * 显示单选对话框
     */
    private void showRadioDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final String[] items = new String[]{
                "a",
                "b",
                "c",
        };
        builder.setTitle("单选对话框").setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                builder1.setMessage("你选择了 【" + items[which] + "】");
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    /**
     * 显示多选对话框
     */
    private void showCheckboxDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("多选对话框");
        final String[] items = new String[]{
                "a",
                "b",
                "c",
        };
        final boolean[] checkedItems = new boolean[]{
                true,
                false,
                false,
        };
        builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                System.out.println("which = " + which);
                System.out.println("isChecked = " + isChecked);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(DialogActivity.this, "你取消了选择", Toast.LENGTH_SHORT).show();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = "";
                for (int i = 0; i < checkedItems.length; i++) {
                    boolean b = checkedItems[i];
                    str += b ? items[i] + "," : "";
                }
                Toast.makeText(DialogActivity.this, "你选择了" + str, Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    /**
     * 显示确定取消对话框
     */
    private void showCancelOkDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示:").setMessage("你是不是个帅哥?");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(DialogActivity.this, "你不是个帅哥", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(DialogActivity.this, "你是个帅哥", Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
}
